package org.example.managere.service.impl;

import jakarta.persistence.EntityNotFoundException;
import org.example.managere.dto.FMEAFMESDto;
import org.example.managere.entity.operationdata.FMEAFMES;
import org.example.managere.repository.operationdata.FMEAFMESRepository;
import org.example.managere.service.FMEAFMESService;
import org.example.managere.util.AdvancedSearchUtil;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FMEAFMESServiceImpl implements FMEAFMESService {

    private final FMEAFMESRepository fmeafmesRepository;
    private final ModelMapper modelMapper;

    @Autowired
    public FMEAFMESServiceImpl(FMEAFMESRepository fmeafmesRepository, ModelMapper modelMapper) {
        this.fmeafmesRepository = fmeafmesRepository;
        this.modelMapper = modelMapper;
    }

    @Override
    public List<FMEAFMESDto> getAllFMEAFMES() {
        return fmeafmesRepository.findAll().stream()
                .map(fmeafmes -> modelMapper.map(fmeafmes, FMEAFMESDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public Page<FMEAFMESDto> getFMEAFMESWithPagination(Pageable pageable) {
        return fmeafmesRepository.findAll(pageable)
                .map(fmeafmes -> modelMapper.map(fmeafmes, FMEAFMESDto.class));
    }

    @Override
    public Page<FMEAFMESDto> searchFMEAFMES(String keyword, Pageable pageable) {
        return fmeafmesRepository.findByNameContaining(keyword, pageable)
                .map(fmeafmes -> modelMapper.map(fmeafmes, FMEAFMESDto.class));
    }

    @Override
    public Page<FMEAFMESDto> advancedSearch(Map<String, String> searchParams, Pageable pageable) {
        return fmeafmesRepository.findAll(AdvancedSearchUtil.buildAdvancedSearchSpecification(searchParams), pageable)
                .map(fmeafmes -> modelMapper.map(fmeafmes, FMEAFMESDto.class));
    }

    @Override
    public FMEAFMESDto getFMEAFMESById(Integer id) {
        FMEAFMES fmeafmes = fmeafmesRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("未找到ID为" + id + "的失效模式和影响分析数据"));
        return modelMapper.map(fmeafmes, FMEAFMESDto.class);
    }

    @Override
    public FMEAFMESDto createFMEAFMES(FMEAFMESDto fmeafmesDto) {
        FMEAFMES fmeafmes = modelMapper.map(fmeafmesDto, FMEAFMES.class);
        FMEAFMES savedFMEAFMES = fmeafmesRepository.save(fmeafmes);
        return modelMapper.map(savedFMEAFMES, FMEAFMESDto.class);
    }

    @Override
    public FMEAFMESDto updateFMEAFMES(Integer id, FMEAFMESDto fmeafmesDto) {
        if (!fmeafmesRepository.existsById(id)) {
            throw new EntityNotFoundException("未找到ID为" + id + "的失效模式和影响分析数据");
        }
        
        FMEAFMES fmeafmes = modelMapper.map(fmeafmesDto, FMEAFMES.class);
        fmeafmes.setId(id);
        FMEAFMES updatedFMEAFMES = fmeafmesRepository.save(fmeafmes);
        return modelMapper.map(updatedFMEAFMES, FMEAFMESDto.class);
    }

    @Override
    public void deleteFMEAFMES(Integer id) {
        if (!fmeafmesRepository.existsById(id)) {
            throw new EntityNotFoundException("未找到ID为" + id + "的失效模式和影响分析数据");
        }
        fmeafmesRepository.deleteById(id);
    }
} 